using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadingPanel : MonoBehaviour
{
    public static LoadingPanel Instance = null;

    //[SerializeField] private Text _loadTxt;
    //ScrambleMode sm = ScrambleMode.All;

    private Slider _slider;

    public void Init()
    {
        Instance = this;
        _slider = transform.Find("Slider").GetComponent<Slider>();
    }

    private void Update()
    {
        prgCB?.Invoke();
    }

    private Action prgCB = null;

    public void LoadSceneAsync(string sceneName)
    {
        _slider.value = 0;
        SetPanelActive(true);
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
        prgCB = () =>
        {
            float val = asyncOperation.progress;
            _slider.value = val;
            if (val == 1)
            {
                asyncOperation = null;
                prgCB = null;
                SetPanelActive(false);
            }
        };
    }

    public void SetPanelActive(bool isActive)
    {
        gameObject.SetActive(isActive);

        // if (isActive)
        // {
        //     _loadTxt.DOText("加载中......", 0.5f, false, sm).SetLoops(-1);
        // }
        // else
        // {
        //     _loadTxt.DOKill();
        // }
    }
}